<template>
  <div class="crontab-container">
      <byui-query-form>
        <el-row :gutter="20" type="flex">
          <div class="list-margin-lr">按日期查询</div>
          <div class="list-margin-lr">
            <el-date-picker
              v-model="time"
              type="daterange"
              align="right"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :picker-options="pickerOptions"
              style="margin-left: -1px;"
              @change="setTime"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </div>
        </el-row>
      </byui-query-form>

    <el-table v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText" border>
      <el-table-column prop="taskName" label="日期" width="300" v-if="checkPermission(['admin','root'])">
        <template slot-scope="scope">
          <div>{{time[0]}}/{{time[1]}}</div>
        </template>
       </el-table-column>
      <el-table-column prop="usetime" label="日期" width="300" v-if="!checkPermission(['admin','root'])"> </el-table-column>
      <el-table-column prop="username" label="用户名" v-if="checkPermission(['admin','root'])" width="260"> </el-table-column>
      <el-table-column prop="commTotal" label="预估总佣金" width=""> </el-table-column>
      <el-table-column prop="tljTotal" label="礼金总支出" width=""> </el-table-column>
      <el-table-column prop="total" label="预估总盈亏" width=""> </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { dataDetails,listCrontab,listUnionChannel,listUnionName } from '@/api/index';
import checkPermission from '@/utils/permission';
  export default {
    name: 'Crontab',
    components: {},
    data() {
      return {
        total:0,
        listForm:{
          startTime:'',
          endTime:'',
        },
        time:[],
        pickerOptions: {
          disabledDate: this.disabledDate,
        },
        listLoading: true,
        list:[],
        elementLoadingText: '正在加载...',
      };
    },
    created() {
      if (this.checkPermission(['admin','root'])) {
        var start = new Date();
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 1);
        this.time = [this.GMTToStr(start),this.GMTToStr(start)];
        this.listForm.startTime = this.time[0];
        this.listForm.endTime = this.time[1];
      }else{
        var end = new Date();
        var start = new Date();
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 11);
        end.setTime(end.getTime() - 3600 * 1000 * 24 * 1);
        this.time = [this.GMTToStr(start),this.GMTToStr(end)];
        this.listForm.startTime = this.time[0];
        this.listForm.endTime = this.time[1];
      }
      this.fetchData();
    },
    mounted() {
    },
    methods: {
      fetchData(){
        dataDetails(this.listForm).then(res=>{
          this.list = res.data;
          this.total = res.totalCount;
          setTimeout(() => {
            this.listLoading = false;
          }, 500);
        })
      },
      disabledDate (value) {
        if (new Date().getTime() - value.getTime() < 86400001) {
          return true
        }
        return false
      },
      setTime(e){
        if(e === null) {
          return false;
        }
        if (this.time && this.time.length == 2) {
          this.listForm.startTime = this.time[0];
          this.listForm.endTime = this.time[1];
        }else{
          this.listForm.startTime = ''
          this.listForm.endTime = ''
        }
        this.fetchData();
      },
      GMTToStr(time){
          let date = new Date(time)
          let Str=date.getFullYear() + '-' +(date.getMonth() + 1) + '-' +date.getDate()
          return Str
      },
      checkPermission
    },
  };

</script>

<style lang="scss" scoped>
    .crontab-container {
      .list-margin-lr {
        margin: 0 10px;
        line-height: 32px;
      }
  }
</style>
